package com.capgemini.rdlgae.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

@SuppressWarnings("serial")
public class CheckOrder extends HttpServlet {
	private static Logger log = Logger.getLogger(CheckOrder.class.getName());

	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String dateString = req.getParameter("date");
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");

		Date date = null;
		try {
			date = sdf.parse(dateString);
			
			

			Query q = new Query("Order");
			
			q.addFilter("date", FilterOperator.EQUAL, date);
			
			
			Entity o = DatastoreServiceFactory.getDatastoreService().prepare(q).asSingleEntity();
			if (o == null || (o != null && ! (Boolean)o.getProperty("lu"))) {
				resp.setStatus(400);
			} else if (o != null && (Boolean)o.getProperty("lu")) {
				resp.setStatus(200);
			}
			

		}catch(ParseException e){
			log.severe(e.getMessage());
		}

	}
}
